package geekstudy.week1;

import java.util.ArrayList;
import java.util.List;

public class Week1RemoveDuplicatesFromSortedArray {

    public int removeDuplicatesWithCollection(int[] nums) {

        List<Integer> targetList = new ArrayList<Integer>();
        List<Integer> redundancyList = new ArrayList<Integer>();

        for(int index = 0; index < nums.length; index++) {
            int value = nums[index];
            if(targetList.contains(value)) {
                redundancyList.add(value);
            } else {
                targetList.add(value);
            }
        }

        int result = targetList.size();
        for(int targetIndex = 0; targetIndex < targetList.size(); targetIndex++) {
            nums[targetIndex] = targetList.get(targetIndex);
        }
        for(int redundancyIndex = 0; redundancyIndex < redundancyList.size(); redundancyIndex++) {
            nums[targetList.size() + redundancyIndex] = redundancyList.get(redundancyIndex);
        }

        return result;
    }


    public int removeDuplicates(int[] nums) {


        if(nums.length == 1) {
            return 1;
        }
        int lastIndex = 0;
        for(int index = 1; index < nums.length; index++) {
            if(nums[lastIndex] != nums[index]) {
                lastIndex++;
                nums[lastIndex] = nums[index];
            }
        }

        int result = lastIndex + 1;
        return result;
    }

}
